var CLAZZ_OID = [];
var JIECI_ID = [];
var COURSE_ID = [];
var TYPE_ID = [];
var STUDENT_ID = [];
function CreateCourseList() {
    coursel = $api.byId('courseList');
    api.ajax({
        url: 'http://222.82.232.219/XSKQ/xskq/comboMyCourse.do',
        method: 'post',
    }, function(ret, err) {
        if (ret) {
            for (i = 0; i < ret.result.length; i++) {
                COURSE_ID.push(ret.result[i].oid);
                coursel.add(new Option(ret.result[i].name, "" + i))
            }
        } else {
            alert(JSON.stringify(err));
        }
    });
}

function CreateGradeList() {
    gradel = $api.byId('gradeList');
    api.ajax({
        url: 'http://222.82.232.219/XSKQ/xskq/listGrade.do',
        method: 'post',
    }, function(ret, err) {
        if (ret) {
            var html = "";
            for (i = 0; i < ret.result.length; i++) {
                gradel.add(new Option(ret.result[i].name, "" + i))
//                html += '<option>' + ret.result[i].name + '</option>'
            }
            $api.append(gradel, html);
        } else {
            alert(JSON.stringify(err));
        }
    });
}

function CreateClazzList() {
    gradel = $api.byId('gradeList');
    clazzl = $api.byId('clazzList');
    var gradeindex = gradel.selectedIndex + 1;
    if(gradeindex == 0)
        gradeindex = 1;
    api.ajax({
        url: 'http://222.82.232.219/XSKQ/xskq/listClazz.do',
        method: 'post',
    }, function(ret, err) {
        if (ret) {
            var k = 0;
            for (var i = 0; i < clazzl.options.length;) {
                clazzl.removeChild(clazzl.options[i]);
            }
            for (i = 0; i < ret.result.length; i++) {
                if (ret.result[i].grade.oid == gradeindex)
                {
                    clazzl.add(new Option(ret.result[i].name, "" + k));
                    CLAZZ_OID[k++] = ret.result[i].oid;
                }
            }
            CreateNameList();
        } else {
            alert(JSON.stringify(err));
        }
    });
}

function CreateJieCiList() {
    jiecil = $api.byId('jieciList');
    api.ajax({
        url: 'http://222.82.232.219/XSKQ/system/listDictionary.do',
        method: 'post',
        data: {
            values: {
                parentId: 45
            }
        }
    }, function(ret, err) {
        if (ret) {
            for (i = 0; i < ret.result.length; i++) {
                JIECI_ID.push(ret.result[i].dicId);
                jiecil.add(new Option(ret.result[i].itemValue, "" + i))
            }
        } else {
            alert(JSON.stringify(err));
        }
    });
}

function CreateTypeList() {
    typel = $api.byId('typeList');
    api.ajax({
        url: 'http://222.82.232.219/XSKQ/system/listDictionary.do',
        method: 'post',
        data: {
            values: {
                parentId: 46
            }
        }
    }, function(ret, err) {
        if (ret) {
            for (i = 0; i <  ret.result.length; i++) {
                TYPE_ID.push(ret.result[i].dicId);
                typel.add(new Option(ret.result[i].itemValue, "" + i))
            }
        } else {
            alert(JSON.stringify(err));
        }
    });
}

function CreateNameList() {
    namel = $api.byId('nameList');
    var clazzindex = clazzl.selectedIndex;
    if(clazzindex < 0) clazzindex = 0;
    api.ajax({
        url: 'http://222.82.232.219/XSKQ/xskq/listStudent.do',
        method: 'post',
        data: {
            values: {
                "Q_EQL_clazz.oid": CLAZZ_OID[clazzindex],
                start:0,
                limit:60
            }
        }
    }, function(ret, err) {
        if (ret) {
            for (var i = 0; i < namel.options.length;) {
                namel.removeChild(namel.options[i]);
            }
            for (i = 0; i < ret.result.length; i++) {
                STUDENT_ID[i] = ret.result[i].oid;
                namel.add(new Option(ret.result[i].name, "" + i))
            }
        } else {
            alert(JSON.stringify(err));
        }
    });
}

function certain() {
    var myDate = new Date();
    var todayDate = myDate.getFullYear() + "-" + (myDate.getMonth() + 1) + "-" + myDate.getDate();
    var jieciindex = jiecil.selectedIndex;
    var courseindex = coursel.selectedIndex;
    var typeindex = typel.selectedIndex;
    var studentindex = namel.selectedIndex;
    var clazzindex = clazzl.selectedIndex;
    api.ajax({
        url: 'http://222.82.232.219/XSKQ/xskq/saveKaoQin.do',
        method: 'post',
        data: {
            values: {
                "kaoQin.cdate": todayDate,
                "kaoQin.jieciType.dicId": JIECI_ID[jieciindex],
                "kaoQin.course.oid": COURSE_ID[courseindex],
                "kaoQin.type.dicId": TYPE_ID[typeindex],
                "student_names": namel.options[studentindex].text,
                "student_ids": STUDENT_ID[studentindex],
                "kaoQin.clazz.oid": CLAZZ_OID[clazzindex]
            }
        }
    }, function(ret, err) {
        if (ret) {
            alert("保存成功！");
        } else {
            alert("保存失败，请检查学生信息！");
        }
    });
}
